package User_webservice;
import java.util.ArrayList;
import java.util.List;
import javax.jws.*;
import javax.persistence.*;

import Entitiy.GeocachingGame;
import Entitiy.Item;
import Entitiy.Team;
import Entitiy.User;
@WebService(portName="AndroidwsPort", serviceName="AndroidWSService",
		targetNamespace="http://User_webservice/",
		endpointInterface="User_webservice.AndroidWS")
public class AndroidWSImplementation implements AndroidWS{
	EntityManager em;
	public AndroidWSImplementation() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("WebService");
		em = emf.createEntityManager();
	}
	
	@Override
	public boolean CreateUser(User user) {
		if(user.getUsername()==null){
			return false;
		}
		List<User> users = em.createQuery("Select users from User users", User.class).getResultList();
		for(User uzer : users){
			if(uzer.getUsername().equals(user.getUsername())){
				return false;
			}
		}
		try{
			em.getTransaction().begin();
			em.persist(user);
			em.getTransaction().commit();
		}catch(Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public User LoginUser(User user){
		List<User> users = em.createQuery("Select users from User users", User.class).getResultList();
		for(User uzer : users){
			if(uzer.getUsername().equals(user.getUsername()) && uzer.getPassword().equals(user.getPassword())){
				return uzer;
			}
		}
		return null;
	}
	@Override
	public boolean CreateGame(GeocachingGame game){
		List<GeocachingGame> games = em.createQuery("Select games from GeocachingGame games",GeocachingGame.class).getResultList();
		if(game.getName() == null){
			return false;
		}
		for(GeocachingGame gam3:games){
			if(game.getName().equals(gam3.getName())){
				return false;
			}
		}
		try{
			em.getTransaction().begin();
			em.persist(game);
			em.getTransaction().commit();
		}catch(Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public List<GeocachingGame> GetGames(){
		return em.createQuery("Select games from GeocachingGame games", GeocachingGame.class).getResultList();
	}
	@Override
	public boolean JoinGame(GeocachingGame game, User user){
		try{
			em.getTransaction().begin();
			if(user.getGames() == null){
				user.setGames(new ArrayList<GeocachingGame>());
			}
			user.getGames().add(game);
			if(game.getUsers()== null){
				game.setUsers(new ArrayList<User>());
			}
			game.getUsers().add(user);
			em.persist(game);
			em.persist(user);
			em.getTransaction().commit();
		}catch(Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public boolean CreateTeam(Team team){
		if(team.getGame()==null || team.getName() == null || team.getPassword() == null){
			return false;
		}
		List<Team> teams = em.createQuery("Select teams from Team teams", Team.class).getResultList();
		for(Team t3am : teams){
			if(t3am.getName().equals(team.getName())){
				return false;
			}
		}
		try{
			em.getTransaction().begin();
			em.persist(team.getGame());
			em.persist(team);
			em.getTransaction().commit();
		}catch(Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public boolean JoinTeam(Team team, User user){
		try{
			em.getTransaction().begin();
			if(team.getUsers()==null){
				team.setUsers(new ArrayList<User>());
			}
			team.getUsers().add(user);
			if(user.getTeams()==null){
				user.setTeams(new ArrayList<Team>());
			}
			user.getTeams().add(team);
			em.persist(user);
			em.persist(team.getGame());
			em.persist(team);
			em.getTransaction().commit();
		}catch (Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public boolean LeaveTeam(Team team, User user){
		try{
			em.getTransaction().begin();
			team.getUsers().remove(user);
			em.persist(user);
			em.persist(team.getGame());
			em.persist(team);
			em.getTransaction().commit();
		}catch (Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public boolean PlaceItem(GeocachingGame game, Item item){
		try{
			em.getTransaction().begin();
			if(game.getItems()==null){
				game.setItems(new ArrayList<Item>());
			}
			game.getItems().add(item);
			em.persist(game);
			em.persist(item);
			em.getTransaction().commit();
		}catch (Exception e){
			return false;
		}finally{
			em.close();
		}
		return true;
	}
	@Override
	public List<Item> RetrieveItems(String name){
		return em.createQuery("Select items from Item items Where items.Name=:NAME", Item.class).setParameter("NAME", name).getResultList();
	}
}
